﻿Public Interface IContentFilter
    Property filter As IContent
    Function doesMatch(content As IContent) As Boolean

End Interface



''' <summary>
''' a class capable to filter contents frome collections
''' </summary>
''' <remarks></remarks>
Public Class ContentFilter(Of Content As IContent)
    Implements IContentFilter

    Public Property filter As Content

    Private Shared content_properties As IEnumerable(Of MysteryPropertyAttribute)

    Public Sub New()
        If content_properties Is Nothing Then
            Dim all As IEnumerable(Of MysteryPropertyAttribute) = getMystery().AssemblyRegister.getMysteryPropertyAttributes(Of Content)()
            content_properties = New HashSet(Of MysteryPropertyAttribute)((From x As MysteryPropertyAttribute In all Select x Where TypeOf (x) Is ContentProperty).ToArray)
            content_properties = New HashSet(Of MysteryPropertyAttribute)((From x As MysteryPropertyAttribute In content_properties Select x Where x.retrive IsNot Nothing).ToArray)

        End If
    End Sub

    ''' <summary>
    ''' return true if the given content matches the filter
    ''' </summary>
    ''' <param name="content"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function doesMatch(content As Content) As Boolean
        For Each cp As MysteryPropertyAttribute In content_properties
            Dim filter_value As ContentPropertyValue = cp.retrive()(filter)
            If filter_value Is Nothing OrElse Not filter_value.isFilter Then Continue For
            Dim filter_instance As IContentPropertyFilter = filter_value
            If Not filter_instance.doesMatch(cp.retrive()(content)) Then Return False
        Next
        Return True
    End Function

    Public Function doesMatch1(content As IContent) As Boolean Implements IContentFilter.doesMatch
        If Not TypeOf (content) Is Content Then Return False
        Return Me.doesMatch(content)
    End Function

    Public Property filter1 As IContent Implements IContentFilter.filter
        Get
            Return filter
        End Get
        Set(value As IContent)
            filter = value
        End Set
    End Property
End Class
